package com.tian.service.impl;

import com.tian.entity.PointsChangeRecord;
import com.tian.entity.UserCouponExpireTemporary;
import com.tian.enums.UserCouponExpirePromptEnum;
import com.tian.enums.UserCouponExpireTemporaryStatus;
import com.tian.mapper.PointsChangeRecordMapper;
import com.tian.mapper.UserCouponExpireTemporaryMapper;
import com.tian.message.UserGetCouponAdditionalMessage;
import com.tian.service.UserGetCouponAdditionalService;
import com.tian.util.ChargeMarketRedisKeyPrefix;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * {@code @description:} 用户领取优惠券 附加处理服务实现类
 *
 * @author tianwc 公众号：Java后端技术全栈
 * 在线刷题 1200+java面试题和1000+篇技术文章：<a href="https://woaijava.cc/">博客地址</a>
 * {@code @date:} 2024-08-20 10:02
 * {@code @version:} 1.0
 */
@Slf4j
@Service
public class UserGetCouponAdditionalServiceImpl implements UserGetCouponAdditionalService {
    @Resource
    private UserCouponExpireTemporaryMapper userCouponExpireTemporaryMapper;
    @Resource
    private PointsChangeRecordMapper pointsChangeRecordMapper;
    @Resource
    private RedissonClient redissonClient;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void process(UserGetCouponAdditionalMessage userGetCouponAdditionalMessage) {
        RBucket<Boolean> bucket = redissonClient.getBucket(ChargeMarketRedisKeyPrefix.RETRY_MESSAGE_REPEAT_PRE + userGetCouponAdditionalMessage.getReqId());
        if (bucket.isExists()) {
            log.info("用户优惠券过期记录已存在，无需处理,userCouponId={}", userGetCouponAdditionalMessage.getUserCouponId());
            return;
        }
        bucket.set(true);
        bucket.expire(1, TimeUnit.DAYS);


        UserCouponExpireTemporary userCouponExpireTemporary = getUserCouponExpireTemporary(userGetCouponAdditionalMessage);
        userCouponExpireTemporaryMapper.insert(userCouponExpireTemporary);
        if (userGetCouponAdditionalMessage.getPoint() != null && userGetCouponAdditionalMessage.getPoint() > 0) {
            PointsChangeRecord pointsChangeRecord = new PointsChangeRecord();
            pointsChangeRecord.setUserId(userGetCouponAdditionalMessage.getUserId());
            pointsChangeRecord.setPoint(userGetCouponAdditionalMessage.getPoint());
            pointsChangeRecord.setType(userGetCouponAdditionalMessage.getType());
            pointsChangeRecord.setCreateTime(userGetCouponAdditionalMessage.getModifyTime());
            pointsChangeRecord.setReqId(userGetCouponAdditionalMessage.getReqId());
            pointsChangeRecordMapper.insert(pointsChangeRecord);
        }
    }

    private UserCouponExpireTemporary getUserCouponExpireTemporary(UserGetCouponAdditionalMessage userGetCouponAdditionalMessage) {
        UserCouponExpireTemporary userCouponExpireTemporary = new UserCouponExpireTemporary();
        userCouponExpireTemporary.setUserCouponId(userGetCouponAdditionalMessage.getUserCouponId());
        userCouponExpireTemporary.setUserId(userGetCouponAdditionalMessage.getUserId());
        userCouponExpireTemporary.setExpireDate(userGetCouponAdditionalMessage.getExpireDate());
        userCouponExpireTemporary.setStatus(UserCouponExpireTemporaryStatus.INIT.getStatus());
        userCouponExpireTemporary.setPrompt(UserCouponExpirePromptEnum.NOT_PROMPT.getPromptType());
        return userCouponExpireTemporary;
    }
}
